using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Text;
using System.Xml;
namespace HKB.Alipay
{
	public class Submit
	{
		private static string _key;
		private static string _input_charset;
		private static string _sign_type;
		static Submit()
		{
			Submit._key = "";
			Submit._input_charset = "";
			Submit._sign_type = "";
			Submit._key = Config.Key.Trim().ToLower();
			Submit._input_charset = Config.Input_charset.Trim().ToLower();
			Submit._sign_type = Config.Sign_type.Trim().ToUpper();
		}
		private static Dictionary<string, string> BuildRequestPara(SortedDictionary<string, string> sParaTemp)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary = Core.FilterPara(sParaTemp);
			string value = Core.BuildMysign(dictionary, Submit._key, Submit._sign_type, Submit._input_charset);
			dictionary.Add("sign", value);
			dictionary.Add("sign_type", Submit._sign_type);
			return dictionary;
		}
		private static string BuildRequestParaToString(SortedDictionary<string, string> sParaTemp, Encoding code)
		{
			Dictionary<string, string> dicArray = new Dictionary<string, string>();
			dicArray = Submit.BuildRequestPara(sParaTemp);
			return Core.CreateLinkStringUrlencode(dicArray, code);
		}
		public static string BuildFormHtml(SortedDictionary<string, string> sParaTemp, string gateway, string strMethod, string strButtonValue)
		{
			Dictionary<string, string> dictionary = new Dictionary<string, string>();
			dictionary = Submit.BuildRequestPara(sParaTemp);
			StringBuilder stringBuilder = new StringBuilder();
			stringBuilder.Append(string.Concat(new string[]
			{
				"<form id='alipaysubmit' name='alipaysubmit' action='",
				gateway,
				"_input_charset=",
				Submit._input_charset,
				"' method='",
				strMethod.ToLower().Trim(),
				"'>"
			}));
			foreach (KeyValuePair<string, string> current in dictionary)
			{
				stringBuilder.Append(string.Concat(new string[]
				{
					"<input type='hidden' name='",
					current.Key,
					"' value='",
					current.Value,
					"'/>"
				}));
			}
			stringBuilder.Append("<input type='submit' value='" + strButtonValue + "' style='display:none;'></form>");
			stringBuilder.Append("<script>document.forms['alipaysubmit'].submit();</script>");
			return stringBuilder.ToString();
		}
		public static XmlDocument SendPostInfo(SortedDictionary<string, string> sParaTemp, string gateway)
		{
			Encoding encoding = Encoding.GetEncoding(Submit._input_charset);
			string s = Submit.BuildRequestParaToString(sParaTemp, encoding);
			byte[] bytes = encoding.GetBytes(s);
			string requestUriString = gateway + "_input_charset=" + Submit._input_charset;
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUriString);
				httpWebRequest.Method = "post";
				httpWebRequest.ContentType = "application/x-www-form-urlencoded";
				httpWebRequest.ContentLength = (long)bytes.Length;
				Stream requestStream = httpWebRequest.GetRequestStream();
				requestStream.Write(bytes, 0, bytes.Length);
				requestStream.Close();
				HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
				Stream responseStream = httpWebResponse.GetResponseStream();
				XmlTextReader reader = new XmlTextReader(responseStream);
				xmlDocument.Load(reader);
			}
			catch (Exception ex)
			{
				string xml = "<error>" + ex.Message + "</error>";
				xmlDocument.LoadXml(xml);
			}
			return xmlDocument;
		}
		public static XmlDocument SendGetInfo(SortedDictionary<string, string> sParaTemp, string gateway)
		{
			Encoding encoding = Encoding.GetEncoding(Submit._input_charset);
			string str = Submit.BuildRequestParaToString(sParaTemp, encoding);
			string requestUriString = gateway + str;
			XmlDocument xmlDocument = new XmlDocument();
			try
			{
				HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(requestUriString);
				httpWebRequest.Method = "get";
				HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
				Stream responseStream = httpWebResponse.GetResponseStream();
				XmlTextReader reader = new XmlTextReader(responseStream);
				xmlDocument.Load(reader);
			}
			catch (Exception ex)
			{
				string xml = "<error>" + ex.Message + "</error>";
				xmlDocument.LoadXml(xml);
			}
			return xmlDocument;
		}
	}
}
